package Classes.GameViews.ViewPanels
{
	import Classes.GameDatas.GD_Player;
	import Classes.GameUtils.GU_filters;
	import Classes.GameUtils.Movement.MovementEvent;
	import Classes.GameUtils.MyTipSprite;
	import Classes.GameUtils.ToolTip_Sprite;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.ViewIcons.ViewIcon;
	import Classes.GameViews.ViewScenes.VS_Main;
	import Classes.Interfaces.I_NeedSWFSource;
	import Classes.Managers.M_Communication;
	import Classes.Managers.M_ConfigData;
	import Classes.Managers.M_LoadSWFSource;
	
	import UIComponents.IUseItem;
	
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.system.ApplicationDomain;
	import flash.utils.Timer;

	/**
	 * 活动面板
	 * @author 
	 * 
	 */	
	public class VP_movement extends ViewPanel implements I_NeedSWFSource
	{
		private static var _instance:VP_movement;
		private var _interfaceUI:Sprite;
		private var _open:Boolean=false;
		public function VP_movement()
		{
//			M_LoadSWFSource.instance.needLoad(this,M_ConfigData.instance.imgUrl+M_ConfigData.instance.main.mainXML.movementPath,"xx");
			M_LoadSWFSource.instance.needLoad(this,M_ConfigData.instance.cdn+M_ConfigData.instance.main.mainXML.movementPath,"xx");
		}
		
		public function init():void
		{
			_interfaceUI	=  	new (ApplicationDomain.currentDomain.getDefinition("MoveMent_v") as Class);
			this.addChild(_interfaceUI);
			
			_interfaceUI.addEventListener("close",closeFun);
			_interfaceUI.addEventListener("btnClick",btnClickHandler);
			
			if( VP_SevenDaysRewardUI.instance.sevensArr==null || VP_SevenDaysRewardUI.instance.sevensArr.length<1)
			{
				var qrdlsp:Sprite	=	this.interfaceUI.getChildByName("content_qrdl") as Sprite;
				qrdlsp.getChildByName("btn1").filters	=	[GU_filters.grayfilter];
				qrdlsp.getChildByName("btn1")["mouseEnabled"] = false;// = ["mouseChildren"]=false;
			}
//			addListener();
			
		}
		
		public function btnClickHandler(e:MovementEvent):void
		{
//			trace( e.listObj[e.btn]);
			this.handlerBtnClick( e.listObj[e.btn]);

		}
		
		private function closeFun(e:Event):void
		{
			GV_Map.instance.buildMode=GV_Map.CANNOTBUILDMODE;
			this.close();
			this._open=false;
		}
		
		public static function get instance():VP_movement
		{
			if(!_instance) _instance=new VP_movement;
			return _instance;
		}
		
		public function get interfaceUI():Sprite
		{
			return _interfaceUI;
		}
		
		private var isInitPosition:Boolean=false;
		public function openPanel():void
		{
			if(!isInitPosition )
			{
				isInitPosition	=	true;
				this.updatePosition();
				this.x=this._backObject.x;
				this.y=this._backObject.y;
			}
			
			
			this._open	=	true;
			
			
			
			if(this._interfaceUI){
				this.updateBtn();
				
				//消费点数
				this.interfaceUI["xfCount"]	=	GD_Player.instance.playerConsume;	
				
				
				GV_Map.instance.buildMode=GV_Map.MOVEMODE;
				
				this.openAt(VS_Main.instance,true);
				this.toCenter();
				
				//查看当前积分
				M_Communication.instance.send(128009);

				//				M_Communication.instance.send(128010);
				
				var it:IUseItem			=	 ( GD_Player.instance.getCurrentWeaponByType(Number(GD_Player.instance.currentWeapon)) as IUseItem);
				var vi:ViewIcon		=	it.itemPic as ViewIcon;
				var newVI:ViewIcon;
				newVI	=	new ViewIcon(vi.sourceName, vi.picName, vi.width, vi.height);
				newVI.numSP["num"]	=	224;
			}
		}
		
		
		public function sourceLoadOver(sourceName:String):void
		{
			init();
			
			
//			this.updatePosition();
//			this.x=this._backObject.x;
//			this.y=this._backObject.y;
			
			if(this._open){
				this.openPanel();
			}
		}
		
		override public function updatePosition():void
		{
			if(this._open)
			super.updatePosition();
			
			this._backObject.scaleX=1;
			this._backObject.scaleY=1;
			this._backObject.x	=	(VP_Main.instance.stage.stageWidth-this.width)/2;
			this._backObject.y	=	-500;
			this.x	=	this._backObject.x;

		}
		
		override protected function toCenter():void
		{
			this.isCenter	=	true;
			this.scaleX		=	this.scaleY		=	1;
			var cx:Number	=	(ZombieTown.instance.sysStage.stageWidth-this.width)/2;
			var cy:Number	=	100;
			this.scaleX=this._backObject.scaleX;
			this.scaleY=this._backObject.scaleY;
			
			TweenLite.to(this,0.5,{scaleX:1,scaleY:1,x:cx,y:cy});
		}
		
		
		public function handlerBtnClick(btnName:String):void
		{
			switch(btnName)
			{
				case "开启礼包":
					command_kqlb();break;
				case "领取奖励":
					command_lqjl();break;
				case "七天大礼":
					command_qtdl();break;
				case "邀请好友":
					command_jqhy();break;
				case "围猎僵尸":
					command_wljs();break;
			}

		}
		
		private var isOldkqlb:Boolean=false;
		/**
		 *开启礼包 
		 * 
		 */
		public function command_kqlb():void
		{
			this.closeFun(null);
			VP_Package.instance.openAt(VS_Main.instance);
//			VP_Package.instance.showCommonItems();
			
			if(!isOldkqlb){			//解决第一次图片变大
				var t:Timer	=	new Timer(2000,1);
				t.addEventListener(TimerEvent.TIMER,function(e:Event):void{
					VP_Package.instance.showCommonItems();
				});
				t.start();
				
				this.isOldkqlb=true;
			}
			else{
				VP_Package.instance.showCommonItems();
			}
		}
		
		/**
		 *领取奖励 
		 * 
		 */
		public function command_lqjl():void
		{
			this.closeFun(null);
//			VP_message.instance.openPanel();
			VP_Main.instance.clickMessageHD(null);
		}
		
		/**
		 *七天大礼 
		 * 
		 */
		public function command_qtdl():void
		{
			this.closeFun(	null);
			VP_SevenDaysRewardUI.instance.open();	
		}
		
		/**
		 *邀请好友 
		 * 
		 */
		public function command_jqhy():void
		{
			ExternalInterface.call("invite");
		}
		
		/**
		 *围猎僵尸-领取奖励 
		 */
		public function command_wljs():void
		{
			M_Communication.instance.send(128010);
		}
		
		/**
		 *当天积分 
		 * @return 
		 * 
		 */
		public function get diescore():uint
		{
			return this.interfaceUI["jsdiecount"];
		}
		
		/**
		 *当天积分 
		 * @param v
		 * 
		 */
		public function set diescore(v:uint):void
		{
			this.interfaceUI["jsdiecount"]	=	v;
		}
		
		/**
		 *围猎僵尸未领的奖励 
		 * @param str
		 * 
		 */
		public function set dieReward(str:String):void
		{
			this.interfaceUI["jsdieNoReward"]		=	str;
		}
		
		/**
		 *围猎僵尸已领的奖励 
		 * @param str
		 * 
		 */
		public function set dieRewardYes(str:String):void
		{
			this.interfaceUI["jsdieYesReward"]		=	str;
		}
		
		/**
		 *消耗点数 
		 * @return 
		 * 
		 */
		public function get consume():uint
		{
			return this.interfaceUI["xfCount"];
		}
		
		/**
		 *消耗点数 
		 * @param v
		 * 
		 */
		public function set consume(v:uint):void
		{
			this.interfaceUI["xfCount"]	=	v;
		}
		
		public function updateBtn():void
		{
			if( this.interfaceUI && this.interfaceUI["nowSelectListObj"])
			{
				this.interfaceUI["xfCount"]	=	GD_Player.instance.playerConsume;	
				this.interfaceUI["setContent"]( this.interfaceUI["nowSelectListObj"]["contentClass"]);
			}
		}
	}
}